const os = require('os');
const { BrowserWindow, ipcMain, dialog } = require('electron');

const logger = require('@utils/logger');
const { readDirRecursively, getPreloadPath } = require('@utils/fileHandle');
const { getLocalIP } = require('@utils/network');
const gs = require('@utils/globalStore')

const { generateHardwareId, generateLicense } = require('@utils/fingerprint');

function getProjectDirectoriesIPC(mainWindow) {
    logger.log('[+10007] IPC event is loading...');

    // reloa-page
    ipcMain.on('reload-page', (event, url) => {
        // console.log('跳转到url:', url);
        const win = BrowserWindow.getFocusedWindow();
        if (win) {
            win.loadURL(url);
        }
    });

    // 通过 ipc 获取 server ip 地址 
    ipcMain.handle('get-network-info', () => {

        const email = 'fu21c@163.com'
        const hardwareId = generateHardwareId();
        const licenseKey = generateLicense(email, hardwareId);

        return {
            ip: getLocalIP(),
            serverPort: gs.getServerPort(),
            clientPort: gs.getClientPort(),
            email: email,
            hostname: os.hostname(),
            serial: licenseKey
        }
    });

    // 获取当前计算机名
    ipcMain.handle('get-computer-name', () => {
        return os.hostname();
    });
}

module.exports = { getProjectDirectoriesIPC };